﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Barbar.Colonization.Model {
  public class CityStore : Building {
    private CityStoreType m_CityStoreType;
    private SortedList<IRawMaterialType, int> m_Materials;

    public CityStoreType CityStoreType {
      get { return m_CityStoreType; }
      protected set { m_CityStoreType = value; }
    }

    public CityStore(CityStoreType cityStoreType) {
      if (cityStoreType == null)
        throw new ArgumentNullException("cityStoreType");
      
      m_CityStoreType = cityStoreType;
      foreach (IRawMaterialType rawMaterialType in m_CityStoreType.RawMaterialTypes) {
        m_Materials.Add(rawMaterialType, 0);
      }
    }

    public bool Consume(RawMaterialCount rawMaterialCount) {
      int currentValue = m_Materials[rawMaterialCount.RawMaterialType];
      if (currentValue < rawMaterialCount.Count)
        return false;
      currentValue -= rawMaterialCount.Count;
      m_Materials[rawMaterialCount.RawMaterialType] = currentValue;
      return true;
    }

    public void Produce(RawMaterialCount rawMaterialCount) {
      int currentValue = m_Materials[rawMaterialCount.RawMaterialType];
      currentValue += rawMaterialCount.Count;
      if (currentValue > m_CityStoreType.MaxCargoPerItem)
        currentValue = m_CityStoreType.MaxCargoPerItem;
      m_Materials[rawMaterialCount.RawMaterialType] = currentValue;      
    }
  }
}
